package com.etc.sfts.controller;

import com.etc.sfts.biz.ManagerBiz;
import com.etc.sfts.biz.UserBiz;
import com.etc.sfts.biz.ipml.ManagerBizImpl;
import com.etc.sfts.entity.Manager;
import com.etc.sfts.util.IsFish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Controller
public class DoUserController {
    @Autowired
    private ManagerBiz biz;
    @Autowired
    private UserBiz userBiz;

    public UserBiz getUserBiz() {
        return userBiz;
    }

    public void setUserBiz(UserBiz userBiz) {
        this.userBiz = userBiz;
    }

    public ManagerBiz getBiz() {
        return biz;
    }

    public void setBiz(ManagerBiz biz) {
        this.biz = biz;
    }
//登录
    @RequestMapping("/Login")
    public void Login(String pwd,String name,HttpServletRequest request, HttpServletResponse response)throws  Exception{

        //设置响应编码
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //调用biz层的方法

        IsFish isFish=new IsFish();
        if(isFish.isFish(request)){
            Manager u=biz.getManagerByNameAndPwd(name, pwd);
            //判断是否为空
            if(u!=null) {
                //把密码和手机号码存在会话域中
                HttpSession session=request.getSession();
                session.setAttribute("Manager", u);
                //登录成功
                out.print(1);
            }else {
                //登录失败
                out.print(0);
            }
        }else {
            out.print(2);
        }

        out.flush();
        out.close();

    }
    //验证码
    @RequestMapping("/checkrand")
    public  void checkrand(String rand,HttpServletRequest request, HttpServletResponse response)throws Exception{

        //从session获取保存的验证码
        String rand2=(String) request.getSession().getAttribute("rand");
        System.out.println(rand);
        System.out.println(rand2);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        if(rand.equals(rand2)) {
            out.print(1);// 验证码匹配
        }else {
            out.print(0); //验证码不匹配
        }
        out.flush();
        out.close();

    }
    //注销
    @RequestMapping("/loginout")
    public  void loginout(HttpServletRequest request, HttpServletResponse response)throws Exception{

        HttpSession session = request.getSession();
        Manager m=(Manager) request.getSession().getAttribute("Manager");
        session.invalidate();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.print("<script>alert('会话已失效，请重新登录');location.href='login.jsp';</script>");
        pw.flush();
        pw.close();
    }
    //获取用户总数
    @RequestMapping("/getUserCount")
    public void getUserCount(HttpServletResponse response)throws Exception{
        Long count=userBiz.getUser();
        PrintWriter out=response.getWriter();
        out.print(count);
        out.flush();
        out.close();

    }

}
